//
//  UIView+Category.m
//  lc-collect
//
//  Created by mac on 2021/3/4.
//

#import "UIView+Category.h"
#import <objc/runtime.h>

NSString const *LCBlockTapKey = @"LCBlockTapKey";
NSString const *LCBlockKey = @"LCBlockKey";

@implementation UIView (Category)

#pragma mark - view 添加点击响应

- (void)lc_addActionWithblock:(TouchCallBackBlock)block {
    
    self.touchCallBackBlock = block;
    self.userInteractionEnabled = YES;
    
    // 添加相同事件方法，，先将原来的事件移除，避免重复调用
    NSMutableArray *taps = [self allUIViewBlockTaps];
    [taps enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UITapGestureRecognizer *tap = (UITapGestureRecognizer *)obj;
        [self removeGestureRecognizer:tap];
    }];
    [taps removeAllObjects];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(invoke:)];
    [self addGestureRecognizer:tap];
    [taps addObject:tap];
}

- (void)invoke:(id)sender {
    
    if(self.touchCallBackBlock) {
        self.touchCallBackBlock();
    }
}

- (void)setTouchCallBackBlock:(TouchCallBackBlock)touchCallBackBlock {
    
    objc_setAssociatedObject(self, &LCBlockKey, touchCallBackBlock, OBJC_ASSOCIATION_COPY);
}

- (TouchCallBackBlock)touchCallBackBlock {
    
    return objc_getAssociatedObject(self, &LCBlockKey);
}

- (void)addTarget:(id)target action:(SEL)action {
    
    self.userInteractionEnabled = YES;
    
    // 添加相同事件方法，，先将原来的事件移除，避免重复调用
    NSMutableArray *taps = [self allUIViewBlockTaps];
    [taps enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UITapGestureRecognizer *tap = (UITapGestureRecognizer *)obj;
        [self removeGestureRecognizer:tap];
    }];
    [taps removeAllObjects];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:target action:action];
    [self addGestureRecognizer:tap];
    [taps addObject:tap];
}

- (NSMutableArray *)allUIViewBlockTaps {
    
    NSMutableArray *taps = objc_getAssociatedObject(self, &LCBlockTapKey);
    if (!taps) {
        taps = [NSMutableArray array];
        objc_setAssociatedObject(self, &LCBlockTapKey, taps, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    return taps;
}

#pragma mark - other

- (UIViewController *)viewController {
    
   UIResponder *responder = self.nextResponder;
   do {
       if ([responder isKindOfClass:[UIViewController class]]) {
           return (UIViewController *)responder;
       }
       responder = responder.nextResponder;
   } while (responder);
    
   return nil;
}

/// 查找第一响应者
- (UIView *)firstResponderView {
    
    if ([self isFirstResponder]) {
        return self;
    }
    
    for (UIView *subview in self.subviews) {
        UIView *firstResponder = subview.firstResponderView;
        if (firstResponder) {
            return firstResponder;
        }
    }
    
    return nil;
}

/// 把View加在Window上
- (void)addToWindow {
    
    id appDelegate = [[UIApplication sharedApplication] delegate];
    if ([appDelegate respondsToSelector:@selector(window)])
    {
        UIWindow * window = (UIWindow *) [appDelegate performSelector:@selector(window)];
        [window addSubview:self];
    }
}

/// 移除所有子视图
- (void)removeAllSubviews {
    
    while (self.subviews.count) {
        [self.subviews.lastObject removeFromSuperview];
    }
}

@end
